package org.ichow.scenegame.controller
{
	import com.greensock.easing.Linear;
	import com.greensock.events.TweenEvent;
	import com.greensock.TimelineMax;
	import com.greensock.TweenMax;
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import org.ichow.debug.Log;
	import org.ichow.scenegame.model.AssetsProxy;
	import org.ichow.scenegame.view.SceneMediator;
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.command.SimpleCommand;

	/**
	 * ...
	 * @author M.ichow
	 */
	public class ScoreEffectCommand extends SimpleCommand
	{

		override public function execute( notification : INotification ) : void
		{
			super.execute( notification );
			var assets : AssetsProxy = facade.retrieveProxy( AssetsProxy.NAME ) as AssetsProxy;
			var scene:SceneMediator = facade.retrieveMediator(SceneMediator.NAME) as SceneMediator;
			var scoreEffect : Class = assets.getClass( "ScoreEffect" );
			var scoreNum : Class = assets.getClass( "ScoreNum" );
			var obj : Object = notification.getBody();
			var se : Sprite = new scoreEffect() as Sprite;
			var sign : MovieClip = se.getChildByName( "sign_mc" ) as MovieClip;
			var nums : Array = String( obj.score ).split( "" );
			var type : String = obj.type;
			var container : DisplayObjectContainer = scene.getViewComponent() as DisplayObjectContainer;
			//
			for ( var i : int = 0 ; i < nums.length ; i++ )
			{
				var item : int = int( nums[ i ] );
				var num : MovieClip = new scoreNum() as MovieClip;
				num.gotoAndStop( item + 1 );
				num.x = ( i + 1 ) * num.width;
				se.addChild( num );
			}
			//
			if ( type == "+" )
			{
				sign.gotoAndStop( 1 );
			}
			else if ( type == "-" )
			{
				sign.gotoAndStop( 2 );
			}
			else
			{
				sign.visible = false;
			}
			// set score effect sprite
			var w:int = int(scene.getValueForKey("width"));
			var h:int = int(scene.getValueForKey("height"));
			se.x = w / 2;
			se.y = h / 2;
			// add to container
			container.addChild( se );
			se.alpha = 0;
			// start 
			var tline : TimelineMax = new TimelineMax({ onComplete: onComplete , onCompleteParams: [ se , container ] } );
			tline.append( new TweenMax( se , .5 , { y: se.y - 20 , alpha: 1 , ease:Linear.easeNone } ) );
			tline.append( new TweenMax( se , 1 , { y: se.y - 60 , ease:Linear.easeNone } ) );
			tline.append( new TweenMax( se , .5 , { y: se.y - 80 , alpha: 0 , ease:Linear.easeNone } ) );
			tline.play();
		}

		private function onComplete( score : DisplayObject , container : DisplayObjectContainer ) : void
		{
			if (container && container.contains(score) )
				container.removeChild(score);
			score = null;
			Log.trace("^^加分动画完成!");
		}
	}

}